﻿// <copyright file="Point3DCollectionConverter.cs" company="Axelerate Solutions S.A.">
// Copyright (c) Axelerate Solutions S.A.  All rights reserved.
// </copyright>

namespace Axelerate.Silverlight3D.Media.Media3D
{
    using System;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.ComponentModel;

    /// <summary>
    /// File name: Point3DCollectionConverter.cs
    /// Company: Axelerate Solutions S.A.
    /// Created By: Daniel Rojas
    /// Created Date: 10/07/2008 12:23:15 h.
    /// Description: Converts instances of other types to and from Point3DCollection instances.
    ///   <Modifications>
    ///   </Modifications>
    /// </summary>
    public class Point3DCollectionConverter : TypeConverter
    {
        /// <summary>
        /// Returns a value that indicates whether the type converter can convert from a specified type.
        /// </summary>
        /// <param name="context">The type descriptor context for this call.</param>
        /// <param name="sourceType">The type being queried for support.</param>
        /// <returns>true if the converter can convert from the provided type; otherwise, false.</returns>
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType.Equals(typeof(string)))
            {
                return true;
            }

            return false;
        }

        /// <summary>
        /// Returns a value that indicates whether the type converter can convert to the specified type.
        /// </summary>
        /// <param name="context">The type descriptor context for this call.</param>
        /// <param name="destinationType">The type being queried for support.</param>
        /// <returns>true if the converter can convert to the provided type; otherwise, false.</returns>
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType.Equals(typeof(string)))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// Attempts to convert to a Point3DCollection from the given object.
        /// </summary>
        /// <param name="context">The type descriptor context for this call.</param>
        /// <param name="culture">The culture information that is respected when converting.</param>
        /// <param name="value">The object to convert to an instance of Point3DCollection.</param>
        /// <returns>The Point3DCollection that was constructed.</returns>
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value is string)
            {
                if (value.ToString().Contains(","))
                {
                    return Point3DCollection.Parse((string)value);
                }
                else
                {
                    if (value.ToString() != "")
                    {
                        string[] pointDataStrings = value.ToString().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        Point3DCollection col = new Point3DCollection();
                        for (int i = 0; i < pointDataStrings.Length; i += 3)
                        {
                            col.Add(new Point3D(double.Parse(pointDataStrings[i]), double.Parse(pointDataStrings[i + 1]), double.Parse(pointDataStrings[i + 2])));
                        }
                        return col;
                    }
                }
            }
            return null;
        }

        /// <summary>
        /// Attempts to convert an instance of Point3DCollection to the specified type
        /// </summary>
        /// <param name="context">The type descriptor context for this call.</param>
        /// <param name="culture">The culture information that is respected when converting.</param>
        /// <param name="value">The object to convert to an instance of destinationType.</param>
        /// <param name="destinationType">Type to which this method will convert the Point3DCollection instance.</param>
        /// <returns>The object that was constructed.</returns>
        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (value is Point3DCollection && destinationType.Equals(typeof(string)))
            {
                return value.ToString();
            }
            return null;
        }
    }
}
